<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="出租出售" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hireSell">
              <a-select v-model="model.hireSell" placeholder="请选择出租出售">
                <a-select-option label="出租" value="出租">出租</a-select-option>
                <a-select-option label="出售" value="出售">出售</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="发布名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineryName">
              <a-input v-model="model.machineryName" placeholder="请输入农机械名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发布描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="machineryDescribe">
              <a-textarea v-model="model.machineryDescribe" rows="4" placeholder="请输入农机械描述" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="描述轮播图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="describeImg">
              <j-image-upload isMultiple v-model="model.describeImg" :number="9"></j-image-upload>
              <div style="font-size: 12px;color:#999;line-height:0">一次上传可选择多张图片！请一次性上传完所需图片！</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="封面图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coverImg">
              <j-image-upload isMultiple v-model="model.coverImg" :number="1"></j-image-upload>
              <div style="font-size: 12px;color:#999;line-height:0">只能上传一张图片</div>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="出租(出售)价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input-number v-model="model.price" placeholder="请输入出租(出售)价格" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="品牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brand">
              <!-- <a-input v-model="model.brand" placeholder="请输入品牌"></a-input> -->
              <j-dict-select-tag type="list" v-model="model.brand" dictCode="machine_brand_type" placeholder="请选择品牌" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="model">
              <!-- <a-input v-model="model.model" placeholder="请输入型号"></a-input> -->
              <j-dict-select-tag type="list" v-model="model.model" dictCode="machine_model_type" placeholder="请选择型号" />
            </a-form-model-item>
          </a-col>

          <!-- <a-col :span="24">
            <a-form-model-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contacts">
              <a-input v-model="model.contacts" placeholder="请输入联系人"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactsPhone">
              <a-input v-model="model.contactsPhone" placeholder="请输入联系电话"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="农机验真" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="inspectionImg">
              <j-image-upload isMultiple v-model="model.inspectionImg" :number="1"></j-image-upload>
              <div style="font-size: 12px;color:#999;line-height:0">只能上传一张图片</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详情介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="describes">
              <j-editor v-model="model.describes"></j-editor>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="农机手" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agricultureName">
              <!-- <a-input v-model="model.agricultureName" placeholder="请输入农机手"></a-input> -->
              <a-select v-model="model.agricultureName" placeholder="请选择农机手">
                <a-select-option v-for="item in machineAgricultureMachinist2" :label="item.agricultureName"
                  :value="item.agricultureName + ',' + item.id">{{
    item.agricultureName
  }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="农机手Id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="agricultureId">
              <a-input v-model="model.agricultureId" placeholder="请输入农机Id"></a-input>
            </a-form-model-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <a-radio-group v-model="model.type" placeholder="请选择状态">
                <a-radio value="1">审核中</a-radio>
                <a-radio value="2">未通过</a-radio>
                <a-radio value="3">通过</a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MachineHireSellForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    machineAgricultureMachinist2: {
      type: Array,
      required: false,
    },
  },
  data() {
    return {
      model: {
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        contactsPhone: [{ required: true, pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!' }],
        // hireSell: [{ required: true, message: '请选择出租出售' }],
        machineryName: [{ required: true, message: '请输入农机械名称' }],
        machineryDescribe: [{ required: true, message: '请输入农机械描述' }],
        describeImg: [{ required: true, message: '请上传描述轮播图' }],
        coverImg: [{ required: true, message: '请上传封面图' }],
        inspectionImg: [{ required: true, message: '请上传农机验真图' }],
        // card: [{ required: true, pattern: /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}([0-9]|X)$/, message: '请输入正确的身份证号码！' }],
        // price: [{ required: true, message: '请输入出租(出售)价格' }],
        brand: [{ required: true, message: '请选择品牌' }],
        model: [{ required: true, message: '请选择型号' }],
        // contacts: [{ required: true, message: '请输入联系人' }],
        describes: [{ required: true, message: '请输入详情介绍' }],
        agricultureName: [{ required: true, message: '请选择农机手' }]
      },
      url: {
        add: "/machine/machineHireSell/add",
        edit: "/machine/machineHireSell/edit",
        queryById: "/machine/machineHireSell/queryById"
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      console.log(record);
      console.log(Object.assign({}, record));
      this.model = Object.assign({}, record);
      // let obj = Object.assign({}, record)
      // obj.agricultureName = obj.agricultureName + ',' + obj.agricultureId
      // this.model = obj;
      this.visible = true;
    },
    submitForm() {
      // console.log('表单', this.model);
      // let arr = this.model.agricultureName.split(',')
      // this.model.agricultureName = arr[0]
      // this.model.agricultureId = arr[1]
      // console.log('新表单', this.model);
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          //处理农机手参数
          let arr = this.model.agricultureName.split(',')
          this.model.agricultureName = arr[0]
          this.model.agricultureId = arr[1]
          //通过
          this.model.type = '3'
          //后台发布
          this.model.category = '1'
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
  }
}
</script>